package com.crm.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.base.ResponseResult;
import com.crm.domain.dto.QueryDictType;
import com.crm.domain.entity.DictType;
import com.crm.service.system.DictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author Xanny
 */
@RestController
@RequestMapping("/system/dict/type")
@RequiredArgsConstructor
public class DictTypeController {

    private final DictTypeService dictTypeService;

    /**
     * 完成字典dictType的查询
     * @return
     */
    @GetMapping("/list")
    public ResponseResult searchList(QueryDictType queryDictType){
        Page<DictType> dictTypeList = dictTypeService.queryAll(queryDictType);
        return ResponseResult.success()
                .put("rows", dictTypeList.getRecords())
                .put("total", dictTypeList.getTotal());
    }

    /**
     * 根据dict_Id查询dict_type
     * @param id
     */
    @GetMapping("{id}")
    public ResponseResult getById(@PathVariable Integer id){
        DictType dictType = dictTypeService.lambdaQuery().eq(DictType::getDictId,id).one();
        return ResponseResult.success(dictType);
    }

    /**
     * 新增字典类型
     * @param dictType
     * @return
     */
    @PostMapping
    public ResponseResult save(@RequestBody DictType dictType){
        dictTypeService.save(dictType);
        return ResponseResult.success();
    }

    /**
     * 修改字典类型
     * @param dictType
     * @return
     */
    @PutMapping
    public ResponseResult update(@RequestBody DictType dictType){
        dictTypeService.lambdaUpdate().eq(DictType::getDictId,dictType.getDictId()).update(dictType);
        return ResponseResult.success();
    }

    /**
     * 删除字典类型
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseResult delete(@PathVariable Integer id){
        dictTypeService.lambdaUpdate().eq(DictType::getDictId,id).remove();
        return ResponseResult.success();
    }
}
